package com.ddm.service.impl.vxImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddm.dto.page.PageQueryDTO;
import com.ddm.entity.Bookmark;
import com.ddm.entity.LevelEntity;
import com.ddm.exception.BaseException;
import com.ddm.mapper.*;
import com.ddm.result.PageResult;
import com.ddm.service.vxService.BookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class BookmarkImpl extends ServiceImpl<BookmarkMapper, Bookmark> implements BookmarkService {

    @Autowired
    private BookmarkMapper bookmarkMapper;
    @Autowired
    private LevelMapper levelMapper;

    /**
     * 分页查询用户收藏的标准 by userId
     *
     * @param pageQueryDTO
     * @param userId
     * @return
     */
    @Override
    public PageResult pageQueryByUserId(PageQueryDTO pageQueryDTO, Long userId) {
        // 创建分页对象，第一个参数为当前页，第二个参数为每页大小
        IPage<Bookmark> page = new Page(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        // 创建 LambdaQueryWrapper，并添加查询条件：userId 等于传入的 userId
        LambdaQueryWrapper<Bookmark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmark::getUserId, userId);
        bookmarkMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<Bookmark> records = page.getRecords();
        //设置数据
        PageResult pageResult = new PageResult(total, records);
        return pageResult;
    }

    /**
     * 查询单个用户收藏的指标
     *
     * @param standardId
     * @param level
     * @param levelId
     * @return
     */
    @Override
    public LevelEntity searchBookmark(Long standardId, Integer level, Long levelId) {
        if (level < 1 || level > 4) {
            throw new BaseException("级别参数错误");
        }
        return levelMapper.selectByParams(levelId, level, standardId);
    }

    /**
     * 删除收藏
     * @param userId
     * @param standardId
     * @param level
     * @param levelId
     */
    @Override
    public int deleteBookmark(Long userId, Long standardId, Integer level, Long levelId){
        QueryWrapper<Bookmark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("standard_id", standardId)
                .eq("level", level)
                .eq("level_id", levelId);
        return bookmarkMapper.delete(queryWrapper);
    }
    
    /**
     * 检查用户是否已报名竞赛
     * @param userId 用户ID
     * @param examId 竞赛ID
     * @return 是否已报名
     */
    @Override
    public boolean checkExamRegistration(Long userId, Long examId) {
        if (userId == null || examId == null) {
            return false;
        }
        
        LambdaQueryWrapper<Bookmark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookmark::getUserId, userId)
                   .eq(Bookmark::getExam_id, examId);
        
        return bookmarkMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 获取用户报名的竞赛ID列表
     * @param userId 用户ID
     * @return 竞赛ID列表
     */
    @Override
    public List<Long> getRegisteredExamIds(Long userId) {
        if (userId == null) {
            return new ArrayList<>();
        }
        
        return bookmarkMapper.selectExamIdsByUserId(userId);
    }
}

